package liuqy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import liuqy.po.Item;
import liuqy.po.ItemCustom;
import liuqy.po.ItemQueryVo;
import liuqy.service.ItemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

//对url进行分类管理，定义根路径，访问根路径+子路径（类内部的maping即为子路径）
@Controller
@RequestMapping("/item")
public class ItemsController {
	@Autowired
	private ItemService itemService;
	
	//商品的查询
	//@RequestMapping("/findItemsList")
	//请求方法可以post也可以get方式，有些基于安全考虑只能进行get方式访问
	@RequestMapping(value="/findItemsList" ,method={RequestMethod.POST,RequestMethod.GET})
	public ModelAndView findItemsList(HttpServletRequest request,ItemQueryVo itemQueryVo) throws Exception{
		//request.setCharacterEncoding("utf-8");
		//System.out.println(new String(itemQueryVo.getItemCustom().getName().getBytes("iso8859-1"),"utf-8"));
		//System.out.println(request.getParameter("id"));
		List<ItemCustom> itemCustoms = itemService.findItemsList(itemQueryVo);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("itemList", itemCustoms);
		modelAndView.setViewName("item/itemList");
		return modelAndView;
	}
	
	//查询商品信息
	@RequestMapping("/selectItemById")
	public ModelAndView selectItemById(Integer id)throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		//调用接口
		ItemCustom itemCustom = itemService.selectItemById(id);
		
		//赋值
		modelAndView.addObject("itemCustom",itemCustom);
		//返回页面
		modelAndView.setViewName("item/editItem");
		return modelAndView;
	}
	
	//查询商品信息
	//通过参数绑定的方式，将页面传入的参数和形参进行绑定
	@RequestMapping("/testParamId")
	//require =true 要求必输
	//defaulltValue 如果没有传输值，将默认值和形参进行绑定
	public ModelAndView testParamId(@RequestParam(value="id",required=true,defaultValue="1") Integer itemId)throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		//调用接口
		ItemCustom itemCustom = itemService.selectItemById(itemId);
		
		//赋值
		modelAndView.addObject("itemCustom",itemCustom);
		//返回页面
		modelAndView.setViewName("item/editItem");
		return modelAndView;
	}
	
	//查询商品信息,Contorller 返回字符串格式
	@RequestMapping("/editItem")
	public String editItem(Integer id,Model model,HttpServletRequest request)throws Exception{
		//调用接口
		ItemCustom itemCustom = itemService.selectItemById(id);		
		//赋值
		model.addAttribute("itemCustom",itemCustom);
		//返回页面
		String flag = request.getParameter("flag");
		if("1".equals(flag)){
			return "item/editItem1";
		}else{
			return "item/editItem2";
		}
	}

	//修改商品信息
	//在需要校验的pojo前面追加@Validated ，后面追加BindingResult
	//@Validated（在前）和BindingResult（在后）配对出现，
	@RequestMapping("/updateItem")
	public ModelAndView updateItem(@RequestParam(value="id")Integer id,
			@Validated ItemCustom itemCustom,BindingResult bindingResult)throws Exception{
		if(bindingResult.hasErrors()){
			//有错误
			List<ObjectError> errors = bindingResult.getAllErrors();
			for(int i = 0; i< errors.size();i++){
				//获取错误信息
				System.out.println(errors.get(i).getDefaultMessage());
			}
		}
		ModelAndView modelAndView = new ModelAndView();
		//调用接口
		itemService.updateItem(id, itemCustom);
		List<ItemCustom> itemCustoms = itemService.findItemsList(null);
		modelAndView.addObject("itemList", itemCustoms);
		modelAndView.setViewName("item/itemList");
		return modelAndView;
	}
	
	@RequestMapping(value="/submitItem",method={RequestMethod.POST})
	public String submitItem(HttpServletRequest request)throws Exception{
		//重定向到商品查询列表
		//return "redirct:findItemsList";
		//forward进行页面转发
		return "forward:findItemsList.action";
	}
	
	@RequestMapping(value="/submitItem1",method={RequestMethod.POST})
	public String submitItem1()throws Exception{
		//重定向到商品查询列表
		return "redirct:findItemsList.action";
	}
	
	
	//void的Controller形式调用
	@RequestMapping(value="/submitItem2",method={RequestMethod.POST})
	public void submitItem2(HttpServletRequest request, HttpServletResponse response)throws Exception{
		//request.getRequestDispatcher("/WEB-INF/item/itemList.jsp").forward(request, response);

		request.getRequestDispatcher("/item/findItemsList.action").forward(request, response);
	}
	@RequestMapping(value="/deleteItems")
	public String deleteItems(Integer [] items_id)throws Exception{
		for(int i=0;i<items_id.length;i++){
			itemService.deleteItem(items_id[i]);
		}
		return "redirct:findItemsList.action";
	}
	
	//批量修改商品页面
	@RequestMapping(value="/batchUpdatePage")
	public ModelAndView batchUpdatePage()throws Exception{
		List<ItemCustom> itemCustoms = itemService.findItemsList(null);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("itemList", itemCustoms);
		modelAndView.setViewName("item/batchUpdateItem");
		return modelAndView;
	}
	
	//批量提交商品页面
	@RequestMapping(value="/batchSubmitItems")
	public String batchSubmitItems(ItemQueryVo  itemCustom)throws Exception{
		for(int i=0;i<itemCustom.getListItems().size();i++){
			itemService.updateItem(itemCustom.getListItems().get(i).getId(), itemCustom.getListItems().get(i));
		}
		return "forward:findItemsList.action";
	}
	

}
